import { Injectable } from '@angular/core';
import { JPushService } from 'ionic2-jpush/dist'

@Injectable()
export class MyJPushService {


  constructor(private jPushPlugin: JPushService) { }

  /*************************************极光******************************************/
  /** 
  * 注册极光 
  */
  jPushInit() {

    this.jPushPlugin.openNotification()
      .subscribe(res => {
        //console.log(res);
        //console.log('收到点击通知事件')
      })

    this.jPushPlugin.receiveNotification()
      .subscribe(res => {
        //console.log(res)
        //console.log('收到通知')
      })

    this.jPushPlugin.receiveMessage()
      .subscribe(res => {
        //console.log(res)
        //console.log('收到自定义消息')
      })

    this.jPushPlugin.backgroundNotification()
      .subscribe(res => {
        //console.log(res)
        //console.log('收到后台通知')
      })



    this.jPushPlugin.init()
      .then(res => {

      })
      .catch(err =>{

      } )
  }

  /** 
  * 获取ID 
  */
  getRegistrationID() {
    this.jPushPlugin.getRegistrationID()
    .then(res => {
      
    })
    .catch(err => {
      
    })
  }
  /**
   * 设置极光别名
   * @param key 
   */
  setAlias(key) {

    let json = {
      sequence: Date.now(),
      alias: key
    };
    try {
      this.jPushPlugin.setAlias(json)
        .then(re => {
          //alert("设置推送======" + JSON.stringify(re));
        })
        .catch(erre => {
          // alert("设置推送失败");
        })
    } catch (e) {
    }

  }
  /*****************************极光 END**********************************************/







}
